package vista;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import constants.Constants;
/**
 * this class will create a new zone for the user to make a revelation 
 * @author Jose Luis Garcia && Alvaro Blazquez Checa
 *
 */
public class revelationPanel extends JFrame implements ActionListener {

	/**
		 * 
		 */
	private static final long serialVersionUID = 1L;

	private JComboBox<String> revelationMode, revelationType, features;
	private JPanel charactheristicsOptions;
	private JButton sendRevelation;
	private JLabel label1, label2, label3, label4, askedPlayer, title1, revela;
	private JList<String> monkNamesList;
	private JScrollPane listContainer;
	private List<String> questions;
	private List<String> monkNames;
	
	/**
	 * this construct will create the panel for revelation 
	 * @param bounds the size for the panel
	 * @param questions features for reveal in the revelation  
	 * @param monkNames monks to the revelation
	 * @param backGroundIcon background Image
	 * @param buttonIcon buttons Image
	 */
	@SuppressWarnings("unchecked")
	public revelationPanel(Rectangle bounds, List<String> questions,List<String> monkNames,Icon backGroundIcon,Icon buttonIcon) {
		this.questions = questions;
		this.monkNames = monkNames;
		setBounds(bounds);
		setLayout(null);
		setUndecorated(true);
		
		Border outsideBorder = BorderFactory
				.createEtchedBorder(EtchedBorder.RAISED);
		Border insideBorder = BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED);

		((JComponent) getContentPane()).setBorder(BorderFactory
				.createCompoundBorder(outsideBorder, insideBorder));

		Font font = new Font("SketchFlow Print", Font.BOLD, 22);
		Font font2 = new Font("SketchFlow Print", Font.BOLD, 14);

		JLabel background=new JLabel(backGroundIcon);
		background.setBounds(3,3,this.getWidth()-6,this.getHeight()-6);
		background.setVisible(true);
		
		JLabel titleRevelation = new JLabel();
		titleRevelation.setLayout(null);
		titleRevelation.setFont(font);
		titleRevelation.setText("Bienvenido al Parlatorium");
		titleRevelation.setForeground(Color.white);
		titleRevelation.setSize(325, 20);
		titleRevelation.setLocation(getWidth()/2 - 325/2, 20);

		label1 = new JLabel();
		label1.setFont(font2);
		label1.setText("HACER UNA REVELACION");
		label1.setOpaque(true);

		label1.setSize(120, 20);
		label1.setLocation(30, 60);

		title1 = new JLabel("Modalidades");
		title1.setForeground(Color.white);
		title1.setBounds(80, 60, 120, 10);
		add(title1);

		revelationType = new JComboBox<String>();
		revelationType.setBounds(90, 80, 200, 20);
		revelationType.addItem("REVELACION");
		revelationType.addItem("ACUSACION");
		revelationType.addActionListener(this);

		revela = new JLabel("El asesino tiene esta caracteristica");
		revela.setForeground(Color.white);
		revela.setBounds(10, 120, 200, 20);

		revela.setVisible(false);

		features = new JComboBox<String>();
		features.setBounds(220, 120, 160, 20);
		setFeatures(questions);

		features.addActionListener(this);
		features.setVisible(false);

		revelationMode = new JComboBox<String>();
		revelationMode = new JComboBox<String>();
		revelationMode.addItem("characteristica");
		revelationMode.addItem("monje");
		revelationMode.setBounds(200, 100, 160, 20);

		sendRevelation = new JButton("Enviar");
		sendRevelation.setName("sendRevelation");
		sendRevelation.setFont(font2);
		sendRevelation.setLayout(null);
		sendRevelation.addActionListener(MainGUI.getInstance());
		sendRevelation.setBounds(80, 145, 269, 30);
		sendRevelation.setIcon(buttonIcon);
		sendRevelation.setVisible(false);
		JLabel buttonText = new JLabel();
		buttonText.setText("Enviar");
		buttonText.setFont(Constants.NORMAL_FONT);
		buttonText.setForeground(Color.white);
		buttonText.setLayout(null);
		buttonText.setBounds(110, 0, 269, 30);

		sendRevelation.add(buttonText);

		monkNamesList = new JList(monkNames.toArray());

		listContainer = new JScrollPane(monkNamesList);
		listContainer.setBounds(10, 180, 380, 200);
		listContainer.setVisible(false);
		add(revela);

		add(titleRevelation);

		add(features);
		add(revelationType);
		add(sendRevelation);

		add(listContainer);
		add(background);
		setVisible(false);

		setAlwaysOnTop(true);
	}
	/**
	 * this method will add the questions to the combo box
	 * @param questions2 
	 */
	private void setFeatures(List<String> questions2) {
		for (String pibot : questions) {
			System.err.println(pibot);
			if (!pibot.equalsIgnoreCase("titulo")
					&& !pibot.equalsIgnoreCase("cabeza")
					&& !pibot.equalsIgnoreCase("cuerpo")
					&& !pibot.equalsIgnoreCase("orden")
					&& !pibot.equalsIgnoreCase("vellofacial")
					&& !pibot.equalsIgnoreCase("|"))
				features.addItem(pibot);
		}
	}
	/**
	 * this will return user choice
	 * @return list of features selected
	 */
	public List<String> getRevelation() {
		List<String> infoList = new ArrayList<String>();
		if (revela.isVisible()) {
			// estoy en el panel de revelaciones
			infoList.add("Revelacion");
			infoList.add("|");
			infoList.add(features.getSelectedItem().toString());
		}

		else {
			// estoy en el de acusaciones
			infoList.add("Acusacion");
			infoList.add("|");
			infoList.add(monkNamesList.getSelectedValue());
		}
		return infoList;
	}

	public String getSilenceVoteAnswer() {

		return revelationMode.getItemAt(revelationMode.getSelectedIndex());

	}

	public void negativeAnswer(String name) {

		label3.setText(name + " hizo voto de silencio");
		label3.setVisible(true);
		JOptionPane.showMessageDialog(this, name + " hizo voto de silencio",
				"VOTO DE SILENCIO", JOptionPane.DEFAULT_OPTION);
		this.setVisible(false);

	}

	public void answerMode(String playerAsking) {

		label1.setVisible(false);
		label3.setVisible(false);
		title1.setVisible(false);
		revela.setVisible(false);
		askedPlayer.setVisible(false);

		sendRevelation.setVisible(false);
		charactheristicsOptions.setVisible(false);
		listContainer.setVisible(false);

		revelationType.setVisible(false);
		features.setVisible(false);

		revelationMode.setVisible(true);
		label2.setVisible(true);
		label4.setText(playerAsking + " te quiere preguntar");
		label4.setVisible(true);

	}

	public void askMode() {

		label1.setVisible(true);

		sendRevelation.setVisible(true);

		charactheristicsOptions.setVisible(false);
		title1.setVisible(false);
		revela.setVisible(false);

		revelationMode.setVisible(false);
		label2.setVisible(false);
		label4.setVisible(false);
		askedPlayer.setVisible(false);
		listContainer.setVisible(false);

		label3.setVisible(false);

		revelationType.setVisible(false);
		features.setVisible(false);

	}

	public void questionsMode(String askedPlayerName) {
		sendRevelation.setVisible(false);

		label3.setVisible(false);

		revelationType.setVisible(true);
		features.setVisible(true);

		askedPlayer.setText(askedPlayerName);
		askedPlayer.setVisible(true);
		listContainer.setVisible(false);

		title1.setVisible(true);
		revela.setVisible(true);

	}
	/**
	 * this method will return selected combo by user 
	 * 
	 * @param list values for the type of revelation
	 * @return true if list is ok
	 */
	public boolean getQuestionValue(List<String> list) {

		boolean questionOk = false;

		String type = (String) revelationType.getSelectedItem();
		String where = (String) features.getSelectedItem();

		// list.add((String)combo1.getSelectedItem());

		list.add(type);
		list.add(where);

		list.add("|");

		if (type.equalsIgnoreCase("Monjes")) {

			if (monkNamesList.getSelectedValue() != null) {
				String monkName = monkNamesList.getSelectedValue();
				list.add(monkName);
				questionOk = true;
			}

		}

		return questionOk;
	}

	private void setQuestionPanel(List<String> questions) {

		charactheristicsOptions = new JPanel();
		charactheristicsOptions.setBounds(10, 180, 380, 200);
		charactheristicsOptions.setBackground(Color.CYAN);

		charactheristicsOptions.setLayout(null);

		Iterator<String> itr = questions.iterator();

		int posX = 10, posY = 20, xtitleSize = 70, xsize = 85, ysize = 20;
		Font f = new Font("SketchFlow Print", Font.PLAIN, 9);
		while (itr.hasNext()) {

			ButtonGroup values = new ButtonGroup();
			// radioButtonsList.add(values);
			List<JRadioButton> listIterable = new ArrayList<JRadioButton>();

			JLabel title = new JLabel(itr.next());
			title.setBounds(posX, posY, xtitleSize, ysize);
			title.setBackground(Color.DARK_GRAY);

			charactheristicsOptions.add(title);

			String value = itr.next();
			posX += 70;
			while (itr.hasNext() && !value.equals("|")) {

				System.out.println(value);
				JRadioButton button = new JRadioButton(value);
				button.setFont(f);
				button.setBounds(posX, posY, xsize, ysize);

				listIterable.add(button);

				// radioButtonsList.put(title.getText(), button);

				values.add(button);
				charactheristicsOptions.add(button);
				value = itr.next();
				posX += 95;
			}

			posX = 10;
			posY += 30;

		}

	}

	public void painComponent(Graphics g) {
		repaint();
	}

	/**
	 * this will handle the type of revelation to make by a selection in the
	 * combo
	 */
	@Override
	public void actionPerformed(ActionEvent e) {

		JComboBox<String> comboBox = (JComboBox<String>) e.getSource();

		String value = (String) comboBox.getSelectedItem();

		if (value.equalsIgnoreCase("REVELACION")) {
			revela.setVisible(true);
			features.setVisible(true);
			sendRevelation.setVisible(true);
			listContainer.setVisible(false);
			getQuestionValue(questions);

		} else if (value.equalsIgnoreCase("ACUSACION")) {
			// charactheristicsOptions.setVisible(false);
			sendRevelation.setVisible(true);
			revela.setVisible(false);
			features.setVisible(false);
			listContainer.setVisible(true);
			getQuestionValue(monkNames);
		}

	}
}
